<?php


namespace App\Http\Controllers\Admin;


use App\Jobs\ImportHeroCast;
use App\Models\Hero;
use App\Services\PositionService;
use Dingo\Api\Http\Request;
use Illuminate\Support\Facades\Storage;

class HeroController extends BaseController
{
    public function index(Request $request) {
        $username = $request->input('username');
        $contact_name = $request->input('contact_name');
        $users = Hero::when($username, function ($q) use($username) {
            return $q->where('username', 'like', '%' . $username . '%');
        })->when($contact_name, function ($q) use($contact_name) {
            return $q->where('contact_name', 'like', '%' . $contact_name . '%');
        })->dontRemember()->paginate(\request('per_page'));
        return $this->paginate($users);
    }

    public function getPosition() {
        return $this->success(PositionService::get());
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id'   => 'required|exists:heros',
        ]);
        $id = \request("id");
        if (!Hero::destroy($id)) {
            return $this->failed('删除失败');
        }
        Hero::flushTags();
        Hero::deleteCachePk($id);
        return $this->success([], '删除成功');
    }

    public function update(Request $request)
    {
        $this->validate($request, [
           'id' => 'required|exists:heros'
        ]);
        $input = $request->input();
        if (!Hero::where('id', $input['id'])->update($input)) {
            return $this->failed('修改失败');
        }
        Hero::flushTags();
        Hero::deleteCachePk($input['id']);
        return $this->success([], '修改成功');
    }

    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:heros'
        ]);
        $id = \request("id");
        return $this->success(Hero::find($id));
    }

    public function import(Request $request) {
        //上传文件最大大小,单位M
        $maxSize = 5;
        //支持的上传图片类型
        $allowed_extensions = ["xls"];
        //返回信息json
        $file = $request->file('file');

        if ($file->isValid()){
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext),$allowed_extensions)){
                $msg = "请上传".implode(",",$allowed_extensions)."格式文件";
                return $this->failed($msg);
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize*1024*1024){
                $msg = "文件大小限制".$maxSize."M";
                return $this->failed($msg);
            }
        }else{
            $msg = $file->getErrorMessage();
            return $this->failed($msg);
        }
        $newFile = "uploads/" . date('Ymd') . "/" . time()."_".uniqid().".".$file->getClientOriginalExtension();
        if (Storage::put($newFile, file_get_contents($file->getRealPath() ))){
            $this->dispatch(new ImportHeroCast($newFile));
            $data = [
                'code'  => 0,
                'msg'   => '上传成功',
                'data'  => $newFile
            ];
            return $this->success($data);
        }

    }

}

